package pw.hais.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import pw.hais.utils.UtilConfig;

/**
 * PopupWindow 窗口 底层
 * Created by hais1992 on 15-5-28.
 */
public abstract class BasePopupWindow extends PopupWindow {
    protected static View popupWindowView; // 菜单弹窗
    protected static Context context = UtilConfig.CONTEXT;
    private Listener listener;


    public void setLayout(int layout,Listener listener){
        popupWindowView = LayoutInflater.from(context).inflate(layout, null); // 获取视图
        setContentView(popupWindowView);
        setWindowLayoutMode(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        setFocusable(true); //获得焦点
        setTouchable(true); //触摸
        setOutsideTouchable(true);  //空白处不退出
        setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null));

        drawEnd(popupWindowView);
        this.listener = listener;
    }

    /**
     * 显示在 view 的中间
     * @param v
     */
    public void showCenter(View v){
        showAtLocation(v, Gravity.CENTER_VERTICAL, 0, 0);
    }



    @Override
    public void dismiss() {
        if(listener!=null)listener.dismissListener();
        super.dismiss();
    }

    /**
     * 绘制完成 的回调
     * @param popupWindowView
     */
    protected abstract void drawEnd(View popupWindowView);

    /**
     * HTTP 请求回调
     */
    public interface Listener{
        void dismissListener();
    }
}
